module material.voltexture;

import render.render;
import material.texture;
import scene.scene;
import scene.ray;
import scene.intersection;
import canvas.abstractcanvas;
import canvas.floatcanvas3d;
import utils.color;
import utils.vector3;
import std.math;
import std.stdio;
import utils.vector2;
import objects.abstractobject;
import objects.trianglemesh;

class VolTexture: Texture {
    FloatCanvas3D mTexture;

    public void texture3d(FloatCanvas3D tex) {
        mTexture = tex;
    }

    public RGBColor getColor(inout RenderContext context, Vector2i offset) {
        RGBColor col;
        if (mTexture) {
            Vector3f p = context.rinters.hitpoint;
            Vector3f sceneSize = context.scene.boundingBox().p[1] -
                context.scene.boundingBox().p[0];
            Vector3f p0 = context.scene.boundingBox().p[0];

            int px = cast(int)((p.x-p0.x)/sceneSize.x * mTexture.width);
            int py = cast(int)((p.y-p0.y)/sceneSize.y * mTexture.height);
            int pz = cast(int)((p.z-p0.z)/sceneSize.z * mTexture.depth);
            if (!(px<0 || px>=mTexture.width
               || py<0 || py>=mTexture.height
               || pz<0 || pz>=mTexture.depth))
            {
                mTexture.getPixelColor(Vector3i(px, py, pz), col);
                return col;
            }
        }
        col = super.getColor(context);
        return col;
    }

    public char[] toString() {
        return "Volume texture";
    }
}
